package terra.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.w3c.dom.Node;

import terra.Config;
import terra.definitions.DropCategory;
import terra.definitions.DropData;
import terra.managers.DefManager;
import terra.model.World;
import terra.model.object.ItemObject;
import terra.model.object.item.Item;
import terra.model.object.map.AbstractMapObject;

public class Util {

	public static Vector<DropCategory> loadDrop(Node node) {
		Vector<DropCategory> list = new Vector<DropCategory>();
		for (Node catNode = node.getFirstChild(); catNode != null; catNode = catNode
				.getNextSibling()) {
			if (catNode.getNodeName() == "category") {
				DropCategory cat = new DropCategory(Integer.parseInt(catNode
						.getAttributes().getNamedItem("id").getNodeValue()));
				for (Node itemNode = catNode.getFirstChild(); itemNode != null; itemNode = itemNode
						.getNextSibling()) {
					if (itemNode.getNodeName() == "item") {
						DropData data = new DropData();
						data.setItemId(Integer.parseInt(itemNode
								.getAttributes().getNamedItem("id")
								.getNodeValue()));
						data.setChance((int) (Float.parseFloat(itemNode
								.getAttributes().getNamedItem("chance")
								.getNodeValue()) * 1000));
						data.setMinDrop(Integer.parseInt(itemNode
								.getAttributes().getNamedItem("minCount")
								.getNodeValue()));
						data.setMaxDrop(Integer.parseInt(itemNode
								.getAttributes().getNamedItem("maxCount")
								.getNodeValue()));
						cat.addDropData(data);
					}
				}
				list.add(cat);
			}
		}
		return list;
	}

	public static List<Item> getDrop(Vector<DropCategory> dropList) {
		ArrayList<Item> list = new ArrayList<Item>();
		for (DropCategory cat : dropList) {
			int chance = MathUtil.rnd.nextInt(Config.MAX_CHANCE);
			if (chance < cat.getCategoryChance()) {
				for (DropData data : cat.getAllDrops()) {
					chance = MathUtil.rnd.nextInt(Config.MAX_CHANCE);
					if (chance < data.getChance()) {
						Item it = new Item(-1,
								DefManager.allItems.get(data.getItemId()), null);
						if (data.getMaxDrop() == data.getMinDrop())
							it.setCount(data.getMinDrop());
						else
							it.setCount(data.getMinDrop()
									+ MathUtil.rnd.nextInt(data.getMaxDrop()
											- data.getMinDrop()));
						list.add(it);
					}
				}
			}
		}
		return list;
	}

	public static boolean doItemDrop(Vector<DropCategory> dropList,
			AbstractMapObject abstractMapObject) {
		List<Item> list = getDrop(dropList);
		for (Item it : list) {
			ItemObject obj = new ItemObject();
			obj.setWidth(Config.tileSize);
			obj.setHeight(Config.tileSize);
			obj.setY(abstractMapObject.getY() - Config.tileSize);
			obj.setX(abstractMapObject.getX());
			obj.setGroundTileX(abstractMapObject.getTileX());
			obj.setGroundTileY(abstractMapObject.getTileY() - 1);
			obj.setDefId(it.getId());
			obj.setItemCount(it.getCount());

			//obj.item = it;
			//obj.setName(obj.item.getName());
			World.getInstance().addObject(obj.getObjectId(), obj);
		}
		return false;
	}

	public static boolean doItemDrop(Vector<DropCategory> dropList, float x,
			float y, int tX, int tY, int mapIndex) {
		List<Item> list = getDrop(dropList);
		for (Item it : list) {
			ItemObject obj = new ItemObject();
			obj.setY(y);
			obj.setX(x);
			obj.setWidth(Config.tileSize);
			obj.setHeight(Config.tileSize);
			obj.setGroundTileX(tX);
			obj.setGroundTileY(tY);
			obj.setDefId(it.getId());
			obj.setItemCount(it.getCount());
			//obj.item = it;
			//obj.setName(obj.item.getName());
			// obj.checkLocation();
			World.getInstance().addObject(obj.getObjectId(), obj);
		}
		return false;
	}

}
